﻿using AutoMapper;
using MediatR;
using Sale.Api.Application.Command.ClientPrice;
using Sale.Domain;
using Sale.ErrorCode;
using Sale.Infrastructure.Implement;

namespace Sale.Api.Application.Handler.ClientPrice
{
    public class DelClientPriceHandler : IRequestHandler<DelClientPriceCommand, ApiResult<int>>
    {
        private readonly DatabaseContext db;
        private readonly ILogger<DelClientPriceHandler> logger;

        public DelClientPriceHandler(DatabaseContext db,ILogger<DelClientPriceHandler> logger)
        {
            this.db = db;
            this.logger = logger;
        }
        public async Task<ApiResult<int>> Handle(DelClientPriceCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();
            var clientPrice = db.SqlSugar.Queryable<ClientPriceModel>().Where(x => x.ClientPriceId == request.ClientPriceId).First();
            clientPrice.IsDel = true;
            int r = await db.SqlSugar.Updateable(clientPrice).ExecuteCommandAsync();
            if (r > 0)
            {
                result.Msg = "客户单价删除成功";
                result.Data = r;
                result.Code = ApiEnum.OK;
                logger.LogInformation(result.Msg);
            }
            else
            {
                result.Msg = "客户单价删除失败";
                result.Data = r;
                result.Code = ApiEnum.Fail;
                logger.LogError(result.Msg);
            }
            return await Task.FromResult(result);
        }
    }
}
